layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * Outbound Order Details Management 出库订单明细表管理
     */
    var OutOrderDetailed = {
        tableId: "outOrderDetailedTable"
    };

    /**
     * Initialize Table Columns 初始化表格的列
     */
    OutOrderDetailed.initColumn = function () {
        return [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'id', hide: true, title: 'Primary Key'},
            {field: 'detailedOu tNumber', sort: true, title: 'Outbound Order No.', fixed: 'left'},
            {field: 'detailedFactory', sort: true, title: 'Factory No.', fixed: 'left'},
            {field: 'detailedGoodscode', sort: true, title: 'Product Code', fixed: 'left'},
            {field: 'detailedGoodsId', hide: true, sort: true, title: 'Product ID'},
            {field: 'detailedGoodsname', sort: true, title: 'Product Name'},
            {field: 'detailedPutbalenum', sort: true, title: 'Bulk Package Qty'},
            {field: 'detailedNumber', sort: true, title: 'Quantity'},
            {field: 'detailedWeight',hide: true, sort: true, title: 'Weight'},
            {field: 'detailedVolume',hide: true, sort: true, title: 'Volume'},
            {field: 'detailedOutWeight', sort: true, title: 'Weight Out'},
            {field: 'detailedOutVolume', sort: true, title: 'Volume Out'},
            {field: 'detailedPrice', sort: true, title: 'Unit Price'},
            {field: 'detailedMoney', sort: true, title: 'Amount'},
            {field: 'detailedBatchnumber', sort: true, title: 'Batch No.'},
            {field: 'detailedDate', sort: true, title: 'Production Date'},
            {field: 'detailedRebubbles', sort: true, title: 'Bulky/Light Goods'},
            {field: 'detailedPacking', sort: true, title: 'Packaging'},
            {field: 'detailedLargeclass', sort: true, title: 'Product Category'},
            {field: 'detailedGoodstype', sort: true, title: 'Product Type'},
            {field: 'detailedPutcondition', sort: true, title: 'Storage Condition'},
            {field: 'detailedNote', sort: true, title: 'Notes'},
            {field: 'version', sort: true, title: 'Version'},
            {field: 'createTime', sort: true, title: 'Creation Time'},
            {field: 'updateTime', sort: true, title: 'Update Time'},
            {field: 'createUser', sort: true, title: 'Creator'},
            {field: 'updateUser', sort: true, title: 'Updater'},
            {align: 'center', toolbar: '#tableBar', title: 'Action'}
        ]];
    };

    /**
     * Click Search Button 点击查询按钮
     */
    OutOrderDetailed.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(OutOrderDetailed.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
                 // Search button click event 搜索按钮点击事件
                 $('#btnSearch').click(function () {
                     OutOrderDetailed.search();
                 });
                 // Add button click event 添加按钮点击事件
                 $('#btnAdd').click(function () {
                     OutOrderDetailed.openAddDlg();
                 });
                 // Export Excel 导出excel
                 $('#btnExp').click(function () {
                     OutOrderDetailed.exportExcel();
                 });
            }
        });
    };

    /**
     * Open Add Dialog 打开添加对话框
     */
    OutOrderDetailed.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/outOrderDetailed/add';
    };

    /**
     * Export Excel Button 导出Excel按钮
     */
    OutOrderDetailed.exportExcel = function () {
        var checkRows = table.checkStatus(OutOrderDetailed.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * Click Edit 点击编辑
     *
     * @param data Row data when button is clicked 点击按钮时的行数据
     */
    OutOrderDetailed.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/outOrderDetailed/edit?id=' + data.id;
    };

    /**
     * Click Delete 点击删除
     *
     * @param data Row data when button is clicked 点击按钮时的行数据
     */
    OutOrderDetailed.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/outOrderDetailed/delete", function (data) {
                Feng.success("Deletion successful!");
                table.reload(OutOrderDetailed.tableId);
            }, function (data) {
                Feng.error("Deletion failed!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("Are you sure to delete the selected data items?", operation);
    };

    // Render table 渲染表格
    var tableResult = table.render({
        elem: '#' + OutOrderDetailed.tableId,
        url: Feng.ctxPath + '/outOrderDetailed/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: OutOrderDetailed.initColumn()
    });

    // Search button click event
    $('#btnSearch').click(function () {
        OutOrderDetailed.search();
    });

    // Add button click event
    $('#btnAdd').click(function () {
        OutOrderDetailed.openAddDlg();
    });

    // Export Excel
    $('#btnExp').click(function () {
        OutOrderDetailed.exportExcel();
    });

    // Toolbar click event 工具条点击事件
    table.on('tool(' + OutOrderDetailed.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            OutOrderDetailed.openEditDlg(data);
        } else if (layEvent === 'delete') {
            OutOrderDetailed.onDeleteItem(data);
        }
    });
});
